//
//  RegisterViewController.m
//  DaFangYa
//
//  Created by design on 15/7/30.
//  Copyright (c) 2015年 HFKJ_LZQ. All rights reserved.
//

#import "RegisterViewController.h"
#import "ADDetailViewController.h"
#import "EditUserHeadViewController.h"
@interface RegisterViewController ()<UITextFieldDelegate>
{
    NSTimer *timer;
    int Second;
    UIImage *normalImage;
    UIImage *selectImage;
    BOOL sendCheckCodeFlag;
}
@end

@implementation RegisterViewController

- (void)viewDidLoad {
    self.titleForNav = @"注册";
    self.isSupportSingleTapHideKeyboard = YES;
    [super viewDidLoad];
    _sendBtn.layer.cornerRadius = 2.0f;
   
    sendCheckCodeFlag = NO;
    normalImage = [UIColor imageFromColor:CGRectMake(0, 0, 100, 30) andColor:[UIColor yellowColor]];
    selectImage =[UIColor imageFromColor:CGRectMake(0, 0, 100, 30) andColor:[UIColor grayColor]]; //NavBarBJColor
    _phoneTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    _textCodeTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    _passwdTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    _nickNameTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    
//    UIButton *disMissBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 15, 60, 40)];
//    [disMissBtn setImage:[UIImage imageNamed:@"arrow_white_left"] forState:UIControlStateNormal];
//    disMissBtn.tag = 103;
//    [disMissBtn addTarget:self action:@selector(disMissHandle:) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:disMissBtn];
    [_phoneTextField becomeFirstResponder];
    
    [_phoneTextField setTextFieldColor:[UIColor blackColor] placeColor:[UIColor blackColor] andStr:@"请输入手机号"];
    [_textCodeTextField setTextFieldColor:[UIColor blackColor] placeColor:[UIColor blackColor] andStr:@"请输入验证码"];
    [_nickNameTextField setTextFieldColor:[UIColor blackColor] placeColor:[UIColor blackColor] andStr:@"请输入昵称"];
    [_passwdTextField setTextFieldColor:[UIColor blackColor] placeColor:[UIColor blackColor] andStr:@"请输入密码"];
    
    NavBarItem *rightItem = [[NavBarItem alloc] init];
    rightItem.type = NavBarItemTypeOnlyStr;
    rightItem.title_normal = @"  下一步";
    rightItem.title_selected = @"  下一步";
    [self setRightNavItemWith:rightItem];
    
}
-(void)rightItemClick:(id)sender
{
    EditUserHeadViewController *head = [[EditUserHeadViewController alloc] initWithNibName:@"EditUserHeadViewController" bundle:nil];
    [self.navigationController pushViewController:head animated:YES];
}
-(void)disMissHandle:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
//    [self.navigationController setNavigationBarHidden:YES animated:YES];
    
    [self registerRequest];
}
//-(void)viewDidLayoutSubviews
//{
//        _phoneTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
//        _textCodeTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
//        _passwdTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
//        _nickNameTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
//        [_phoneTextField setTextFieldColor:[UIColor whiteColor] placeColor:[UIColor whiteColor] andStr:@"请输入手机号"];
//        [_textCodeTextField setTextFieldColor:[UIColor whiteColor] placeColor:[UIColor whiteColor] andStr:@"请输入验证码"];
//        [_nickNameTextField setTextFieldColor:[UIColor whiteColor] placeColor:[UIColor whiteColor] andStr:@"请输入昵称"];
//        [_passwdTextField setTextFieldColor:[UIColor whiteColor] placeColor:[UIColor whiteColor] andStr:@"请输入密码"];
//
//}
//
//-(void)viewWillDisappear:(BOOL)animated
//{
//    [super viewWillDisappear:YES];
//    [self.navigationController setNavigationBarHidden:YES animated:YES];
//    
//}



- (IBAction)btnClickedHandle:(UIButton *)sender {
    switch (sender.tag) {
        case 100:
        {
            //发送验证码
            if (![NSString isMobileNumber:_phoneTextField.text]) {
                [MBProgressHUD showHUDAddedTo:self.view WithString:@"请输入正确的手机号"];
            }
            else
            {
                sendCheckCodeFlag = YES;
                [self checkPhoneIsRegister];
                
            }
            
        }
            break;
        case 101:
        {
            //勾选注册协议
            
        }
            break;
        case 102:
        {
            //注册协议
            ADDetailViewController *ad = [[ADDetailViewController alloc] init];
        
            ad.titleStr = @"注册协议";
            ad.web_url = @"http://www.baidu.com";
            [self.navigationController pushViewController:ad animated:YES];
        }
            break;
            
        default:
            break;
    }
    
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.view endEditing:YES];
    return YES;
}

-(void)sendCheckCodeRequest
{
   
}
-(void)registerRequest
{
    //
//    NSDictionary *paramDic = [[NSDictionary alloc] initWithObjectsAndKeys:_phoneTextField.text,@"account",_passwdTextField.text,@"password",_textCodeTextField.text,@"phoneCode", nil];
     NSDictionary *paramDic = [[NSDictionary alloc] initWithObjectsAndKeys:@"18616707730",@"phone",@"123456",@"pwd",@"lzq",@"nickname",@"111111",@"code",@"1",@"test", nil];
    [[CDAPIManager sharedManager] request_AllHandle_WithParams:paramDic andApiTag:KRegisterTag andIsSign:YES andBlock:^(id data, NSError *error,BOOL isSuccess){
        
        if (isSuccess) {
            
            Person *_person = [Person sharePerson];
            _person.userId = [data objectForKey:@"data"];//得到userid
        }
        else
        {
            
        }
    }];
}

-(void)checkPhoneIsRegister
{

}

- (void)repeats
{
    
    if (Second >0)
    {  --Second;
        [self.sendBtn setBackgroundImage:selectImage forState:UIControlStateNormal];
        self.sendBtn.titleLabel.text =[NSString stringWithFormat:@"重发(%i)s",Second];
        [_sendBtn setTitle:[NSString stringWithFormat:@"重发(%i)s",Second] forState:UIControlStateNormal];
        self.sendBtn.userInteractionEnabled =NO;

        
        // [self.fasongyanzhenmaButton setTitle:[NSString stringWithFormat:@"重发(%i)s",Second] forState:UIControlStateNormal];
        
    }
    else
    {
        [self.sendBtn setBackgroundImage:normalImage forState:UIControlStateNormal];
        self.sendBtn.userInteractionEnabled =YES;
        
        [self.sendBtn setTitle:[NSString stringWithFormat:@"重新发送"] forState:UIControlStateNormal];
        
    }
    
}

- (void)loginRequest
{/* 13296729578  123456 */
    //登录校验都是 在头部信息里面去校验的。。通过能不能拿到用户信息去判断是不是调用登录成功
   
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
